package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.model.ActionPageInfoModel;
import com.hangzhou.xyj.net.handler.GetListOfInfoHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetListOfInfoReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.FindShowRecyclerAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * Created by yaohuix on 2016/4/23  9:44
 * <p/>
 * 说明：
 */
public class MoreShowActivity extends BaseActivity {
    private String mTitle;//标题
    private RecyclerView mRecyclerView;
    private FindShowRecyclerAdapter mAdapter;
    private int mCatId;//剧种ID
    private int mPageSize = 10;
    private List<ActionModel> mDatas;

    @Override
    public void initIntentData(Intent intent) {
        mTitle = intent.getStringExtra(DataKey.TITLE);
        mCatId = intent.getIntExtra(DataKey.INDEX, -1);
        mTitle = "演出-" + mTitle;
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int currentPage = mAdapter.getCurrentPage(mPageSize);
                        Log.d(TAG, "page == " + currentPage);
                        getShowList(currentPage);
                    }
                }, 500);
            }
        });
    }

    @Override
    public void initDatas() {
        mAdapter.setmPageSize(mPageSize);
        getShowList(1);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_common_recycleview);
    }

    @Override
    public void initTop() {
        enableTitle(true, mTitle);
    }

    @Override
    public void initViews() {
        enableRefresh(true);//可以下拉刷新
        mRecyclerView = findView(R.id.rv_common);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mAdapter = new FindShowRecyclerAdapter(mRecyclerView, null));
    }

    /**
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/16 10:55
     */
    public void getShowList(final int currentPage) {
        //请求数据
        GetListOfInfoReq getListOfInfoReq = new GetListOfInfoReq(GetListOfInfoReq.TYPE_SHOW, mCatId, currentPage, mPageSize);
        HttpLoader.getInstance().sendRequest(new GetListOfInfoHandler(getListOfInfoReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mDatas = ((ActionPageInfoModel) model).getActionList();
                if (currentPage == 1) {
                    mAdapter.load(mDatas);
                } else {
                    mAdapter.update(mDatas);
                }
            }
            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }
}
